/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.workingtimemanager.urb;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import de.workingtimemanager.urb.db.DatabaseHandler;
import de.workingtimemanager.urb.entities.Project;
import de.workingtimemanager.urb.entities.User;

/**
 *
 * @author CK05102
 */
public class CreateProjectActivity extends Activity {

    private DatabaseHandler db;
    private TextView name;
    private TextView number;
    private Button saveButton;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        // ToDo add your GUI initialization code here   
        setContentView(R.layout.createproject);
        db = new DatabaseHandler();
        name = (TextView) findViewById(R.id.createproject_name_text);
        number = (TextView) findViewById(R.id.createproject_number_text);
        saveButton = (Button) findViewById(R.id.createproject_save_button);
        saveButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                createProject(v);
            }
        });
    }

    private void createProject(View v) {
        if (!name.getText().equals(getString(R.string.empty))
                && !number.getText().equals(getString(R.string.empty))) {
            Project project = new Project(name.getText().toString(),
                    number.getText().toString());
            try {
                db.open(this);
                User activeUser = db.getActiveUser();
                activeUser.addProject(project);
                db.insert(activeUser);
            } finally {
                db.close();
            }

            Toast.makeText(this, "Project Created", 3000).show();
            this.finish();
        }
    }
}
